package com.googlecode.stockhamster.bank;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Account {
	private String username;
	private String bankCode;
	private long money;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public static Account get(String username) {
		try {
			String sqlGetAccount = "select * from BankAccount where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setString(1, username);
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			Account tempAccount = new Account();
			tempAccount.setUsername(rs.getString(1));
			tempAccount.setBankCode(rs.getString(2));
			tempAccount.setMoney(rs.getLong(3));
			state.close();
			rs.close();
			return tempAccount;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
			}
		}
		return null;
	}

	public static List<String> getList() {
		List<String> tempAccount = new ArrayList<String>();
		try {
			String sqlGetAccountList = "select * from BankAccount";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			rs = state.executeQuery();
			while (rs.next()) {
				String ac = rs.getString(1);
				tempAccount.add(ac);
			}
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
			}
		}
		return tempAccount;
	}

	public static Account[] getAll() {
		Account[] targetList = new Account[0];
		try {
			ArrayList<Account> tempAccount = new ArrayList<Account>();
			String sqlGetAccountList = "select * from BankAccount";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			rs = state.executeQuery();
			while (rs.next()) {
				Account ac = new Account();
				ac.setUsername(rs.getString(1));
				ac.setBankCode(rs.getString(2));
				ac.setMoney(rs.getLong(3));
				tempAccount.add(ac);
			}
			targetList = new Account[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i);
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
			}
		}
		return targetList;
	}

	public boolean save() {
		boolean flag = false;
		try {
			String sqlSetArbiterAccount = "update BankAccount set money=?, bankcode=? where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlSetArbiterAccount);
			state.setString(3, getUsername());
			state.setString(2, getBankCode());
			state.setLong(1, getMoney());
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	public boolean insert() {
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into BankAccount(username, bankcode, money) values(?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setString(1, getUsername());
			state.setString(2, getBankCode());
			state.setLong(3, getMoney());
			state.executeUpdate();
			flag = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public boolean delete() {
		boolean flag = false;
		try {
			String sqlDelAccount = "delete from BankAccount where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlDelAccount);
			state.setString(1, getUsername());
			state.executeUpdate();
			flag = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getBankCode() {
		return bankCode;
	}

	public void setBankCode(String bankCode) {
		this.bankCode = bankCode;
	}

	public long getMoney() {
		return money;
	}

	public void setMoney(Long money) {
		this.money = money;
	}
}
